package com.jxs.android.weixi;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.TreeMap;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;

import com.jxs.android.util.HandlerHolder;
import com.jxs.android.util.MemoryUtil;
import com.jxs.android.util.StringUtils;
import com.jxs.android.weiget.BMonthView;
import com.jxs.android.weiget.BuAccountInfoPageView;
import com.jxs.android.weiget.BuCustomerView;
import com.jxs.android.weiget.BuLoadingDailog;
import com.jxs.android.weiget.BuProductView;
import com.jxs.android.weiget.BuSalesItem;
import com.jxs.android.weiget.BuSalesView;
import com.jxs.android.weiget.ScrollContainer;
import com.jxs.android.weiget.TableFormView;
import com.jxs.android.weiget.TopBar;
import com.jxs.android.weiget.WheelDateDialog;
import com.jxs.android.weixi.data.AccountInfo;
import com.jxs.android.weixi.data.AttrBaseInfo;
import com.jxs.android.weixi.data.CustBaseInfoNew;
import com.jxs.android.weixi.data.MonthAcctBaseInfo;
import com.jxs.android.weixi.data.ProductBaseInfo;
import com.jxs.android.weixi.data.SalesBaseInfo;
import com.telecom.weixi.R;
import com.telecom.weixi.util.Utils;

@SuppressLint("HandlerLeak")
public class Search_Result_HomePages extends AbstractActivity implements OnPageChangeListener, BuAccountInfoPageView.LoadingDataListener {

	static public enum PageIndex {
		CUSTOMER, PRODUCT, SALES, ATTACH, MONTH, ACCOUT, OWE_SEARCH_RESULT, OWE_SEACHER_DETAIL;
	}

	private ViewPager viewPager;
	private Search_Result_HomePageAdapter viewPagerAdapter;

	private RadioGroup radioGroup;

	private String mServId;
	private String mCustId;
	private String customerNumber;
	public static final String TAG_SERV_ID = "serv_id";
	public static final String TAG_CUST_ID = "cust_id";
	public static final String TAG_CUST_NBR = "cust_nbr";

	private String SPLIT_BLANK = "             ";
	private String SPLIT_BLANK_SUB = "     ";

	private List<Boolean> loadStates;
	private Search_GetCustBaseInfoNewHolder getCustBaseInfoNewHolder;
	private Search_GetProductBaseInfoHolder getProductBaseInfoHolder;
	private Search_GetSalesBaseInfoHolder getSalesBaseInfoHolder;
	private Search_GetAttrBaseInfoHolder getAttrBaseInfoHolder;
	private Search_GetMonthAcctBaseInfoHolder getMonthAcctBaseInfoHolder;
	private Search_GetAccountInfoPageHolder getAccountInfoPageHolder;
	private TopBar topBar;

	private BuCustomerView customerView;
	private BuProductView productView;
	private BuSalesView mBuSalesView;
	private TableFormView attrTableView;
	private BuAccountInfoPageView accountInfoPageView;
	private List<ScrollContainer> containers;
	private final int BASE_PAGE_SIZE = 13;
	private int PAGE_SIZE = BASE_PAGE_SIZE;
	private int currentPage = 1;
	private int mTabIndex;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		initTopBar(savedInstanceState, R.layout.bu_search_result_home_page);

		Bundle data = getIntent().getExtras();
		if (data != null) {
			mServId = data.getString(TAG_SERV_ID);
			mCustId = data.getString(TAG_CUST_ID);
			customerNumber = data.getString(TAG_CUST_NBR);
			if (data.getBoolean("hindTitle", false)) {
				topBar.hindAll();
			}
		}
		initloadState();
		initWaitDialog();
		setupFlowView();
		initSearchPageHolder();
		getCustBaseInfoNewHolder.getCustBaseInfoNew(WeixiApplication.getUserInfo().getTicket(), mCustId, mServId);
	}

	private void initSearchPageHolder() {
		getCustBaseInfoNewHolder = new Search_GetCustBaseInfoNewHolder(handler, PageIndex.CUSTOMER.ordinal());
		getProductBaseInfoHolder = new Search_GetProductBaseInfoHolder(handler, PageIndex.PRODUCT.ordinal());
		getSalesBaseInfoHolder = new Search_GetSalesBaseInfoHolder(handler, PageIndex.SALES.ordinal());
		getAttrBaseInfoHolder = new Search_GetAttrBaseInfoHolder(handler, PageIndex.ATTACH.ordinal());
		getMonthAcctBaseInfoHolder = new Search_GetMonthAcctBaseInfoHolder(handler, PageIndex.MONTH.ordinal());
		getAccountInfoPageHolder = new Search_GetAccountInfoPageHolder(handler, PageIndex.ACCOUT.ordinal());
	}

	private void initloadState() {
		loadStates = new ArrayList<Boolean>();
		for (int index = 0; index < 6; index++) {
			loadStates.add(false);
		}
		containers = new ArrayList<ScrollContainer>();
	}

	private void setLoadState(int pos, boolean isLoad) {
		containers.get(pos).showLoaing(!isLoad);
		loadStates.set(pos, isLoad);
	}

	@Override
	public void topBarLogic(TopBar topBar) {
		super.topBarLogic(topBar);
		this.topBar = topBar;
		topBar.setTitle(R.string.view_customer);
		topBarReturn(topBar);
		topBar.hindBtn();
	}

	private void setupFlowView() {
		topBar.setSubTitle(customerNumber);
		radioGroup = (RadioGroup) findViewById(R.id.cutomer_tabs);
		radioGroup.setOnCheckedChangeListener(tabChangedListener);

		viewPager = (ViewPager) findViewById(R.id.viewPager_search);
		initViewPager();
	}

	private void initViewPager() {
		viewPager.setOnPageChangeListener(this);
		viewPagerAdapter = new Search_Result_HomePageAdapter();

		LayoutParams lp = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
		// cust tab
		ScrollContainer customerViewContainer = new ScrollContainer(this);
		containers.add(customerViewContainer);
		customerView = new BuCustomerView(this);
		customerViewContainer.addView(customerView, lp);
		viewPagerAdapter.addItem(customerViewContainer);

		// product tab
		ScrollContainer productContainer = new ScrollContainer(this);
		containers.add(productContainer);
		productView = new BuProductView(this);
		productContainer.addView(productView, lp);
		viewPagerAdapter.addItem(productContainer);

		// sales tab
		ScrollContainer salesContainer = new ScrollContainer(this);
		containers.add(salesContainer);
		salesContainer.addView(initSalesTableView());
		viewPagerAdapter.addItem(salesContainer);

		// attach tab
		ScrollContainer attrTableViewContainer = new ScrollContainer(this);
		containers.add(attrTableViewContainer);
		attrTableViewContainer.addView(getAttrTableView());
		viewPagerAdapter.addItem(attrTableViewContainer);

		// month
		ScrollContainer monthTableViewContainer = new ScrollContainer(this);
		containers.add(monthTableViewContainer);
		monthTableViewContainer.addView(getMonthTableView());
		viewPagerAdapter.addItem(monthTableViewContainer);

		// account
		ScrollContainer accountTableViewContainer = new ScrollContainer(this);
		containers.add(accountTableViewContainer);
		accountTableViewContainer.addView(getAccountTableView());
		viewPagerAdapter.addItem(accountTableViewContainer);

		viewPager.setAdapter(viewPagerAdapter);
	}

	private View getAccountTableView() {
		accountInfoPageView = new BuAccountInfoPageView(this);
		accountInfoPageView.setDataListener(this);
		return accountInfoPageView;
	}

	private OnCheckedChangeListener tabChangedListener = new OnCheckedChangeListener() {
		@Override
		public void onCheckedChanged(RadioGroup group, int checkedId) {
			switch (checkedId) {
			case R.id.tab_customer:
				viewPager.setCurrentItem(PageIndex.CUSTOMER.ordinal());
				break;
			case R.id.tab_product:
				viewPager.setCurrentItem(PageIndex.PRODUCT.ordinal());
				break;
			case R.id.tab_sales:
				viewPager.setCurrentItem(PageIndex.SALES.ordinal());
				break;
			case R.id.tab_attach:
				viewPager.setCurrentItem(PageIndex.ATTACH.ordinal());
				break;
			case R.id.tab_month:
				viewPager.setCurrentItem(PageIndex.MONTH.ordinal());
				break;
			case R.id.tab_business:
				viewPager.setCurrentItem(PageIndex.ACCOUT.ordinal());
				break;
			}
		}
	};

	@Override
	public void onPageScrollStateChanged(int state) {
		if (state == ViewPager.SCROLL_STATE_IDLE) {
			if (!loadStates.get(mTabIndex)) {
				if (mTabIndex == PageIndex.CUSTOMER.ordinal()) {
					getCustBaseInfoNewHolder.getCustBaseInfoNew(WeixiApplication.getUserInfo().getTicket(), mCustId, mServId);
				} else if (mTabIndex == PageIndex.PRODUCT.ordinal()) {
					getProductBaseInfoHolder.getProductBaseInfo(WeixiApplication.getUserInfo().getUserId(), mServId);
				} else if (mTabIndex == PageIndex.SALES.ordinal()) {
					getSalesBaseInfoHolder.getSalesBaseInfo(WeixiApplication.getUserInfo().getUserId(), mServId);
				} else if (mTabIndex == PageIndex.ATTACH.ordinal()) {
					getAttrBaseInfoHolder.getAttrBaseInfo(WeixiApplication.getUserInfo().getUserId(), mServId);
				} else if (mTabIndex == PageIndex.MONTH.ordinal()) {
					getMonthInfo(getDateYM());
				} else if (mTabIndex == PageIndex.ACCOUT.ordinal()) {
					loadingAccountInfo(currentPage);
				}
			}
		}
	}

	@Override
	public void loadingData(int currentPage) {
		loadingAccountInfo(currentPage);
	}

	private void loadingAccountInfo(final int currentPage) {
		getAccountInfoPageHolder.getAccountInfoPage(WeixiApplication.getUserInfo().getTicket(), mServId, PAGE_SIZE, currentPage);
	}

	private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			if (MemoryUtil.isCurrentActivity(Search_Result_HomePages.class)) {
				if (msg.what == HandlerHolder.What.UPDATE_LIST_UI) {
					if (msg.arg1 == PageIndex.CUSTOMER.ordinal()) {
						updateCustBaseInfoNewList(msg);
					} else if (msg.arg1 == PageIndex.PRODUCT.ordinal()) {
						updateProductBaseInfoList(msg);
					} else if (msg.arg1 == PageIndex.SALES.ordinal()) {
						updateSalesBaseInfoList(msg);
					} else if (msg.arg1 == PageIndex.ATTACH.ordinal()) {
						updateAttrBaseInfoList(msg);
					} else if (msg.arg1 == PageIndex.MONTH.ordinal()) {
						updateMonthBaseInfoList(msg);
					} else if (msg.arg1 == PageIndex.ACCOUT.ordinal()) {
						updateAccountInfoList(msg);
					}
				} else if (msg.what == HandlerHolder.What.ERROR_MSG) {
					errorMsgPop(msg.obj.toString());
				} else if (msg.what == HandlerHolder.What.TO_LOGIN) {
					errorMsgToLogin(msg.obj.toString());
				}
			}
		}
	};

	private void updateProductBaseInfoList(Message msg) {
		setLoadState(1, true);
		@SuppressWarnings("unchecked")
		TreeMap<String, ProductBaseInfo> mProducts = (TreeMap<String, ProductBaseInfo>) msg.obj;
		/**
		 * [1-用户姓名-康世英, 2-用户层客户类型-公众, 3-用户状态-正常, 4-入网日期-20101004, 5-在网时长-27月,
		 * 6-区域-临夏州_临夏市中心, 7-地址-甘肃省广河县城关镇赵家村拱北143号, 8-用户备注-用户申请办理取消短信功能,
		 * 9-联系人名称-康世英, 10-联系人号码-0, 11-联系人地址-甘肃省广河县城关镇赵家村拱北143号]
		 */
		Log.e("mProducts", mProducts.toString());
		for (String key : mProducts.keySet()) {
			if ("1".equals(key))
				productView.setProdName(mProducts.get(key).content);
			else if ("2".equals(key))
				productView.setProdType(mProducts.get(key).content);
			else if ("3".equals(key))
				productView.setProdState(mProducts.get(key).content);
			else if ("4".equals(key))
				productView.setJoinDate(mProducts.get(key).content);
			else if ("5".equals(key))
				productView.setJoinAge(mProducts.get(key).content);
			else if ("6".equals(key))
				productView.setNetGrid(mProducts.get(key).content);
			else if ("7".equals(key))
				productView.setUserAddr(mProducts.get(key).content);
			else if ("8".equals(key))
				productView.setUserExtra(mProducts.get(key).content);
			else if ("9".equals(key))
				productView.setConect_name(mProducts.get(key).content);
			else if ("10".equals(key))
				productView.setConect_phone(mProducts.get(key).content);
			else if ("11".equals(key))
				productView.setConect_address(mProducts.get(key).content);
		}

	}

	private void updateCustBaseInfoNewList(Message msg) {
		setLoadState(0, true);
		@SuppressWarnings("unchecked")
		TreeMap<String, CustBaseInfoNew> mCustInfos = (TreeMap<String, CustBaseInfoNew>) msg.obj;
		String name = "", type = "", manager = "";
		StringBuffer kehuNumBuffer = new StringBuffer("");
		StringBuffer zhanghuNumBuffer = new StringBuffer("");
		String zhanghuList = "";
		String kehulist = "";
		for (String key : mCustInfos.keySet()) {
			if ("2".equals(key)) {// @2 = "";// '客户名称'
				name = mCustInfos.get(key).content;
			} else if ("3".equals(key)) {// @3 = "";// '客户层客户类型'
				type = mCustInfos.get(key).content;
			} else if ("4".equals(key)) {// @4 = "";// '客户层客户经理'
				manager = mCustInfos.get(key).content;
			} else if ("5".equals(key)) {
				/**
				 * @5 = "";// '客户产品数'
				 * @6 = "";// '移动'
				 * @7 = "";// '固话'
				 * @8 = "";// '宽带'
				 * @9 = "";// '其它'
				 */
				kehuNumBuffer.append(mCustInfos.get(key).content + " 【");
			} else if ("6".equals(key)) {
				kehuNumBuffer.append(mCustInfos.get(key).title + mCustInfos.get(key).content);
			} else if ("7".equals(key)) {
				kehuNumBuffer.append(mCustInfos.get(key).title + mCustInfos.get(key).content);
			} else if ("8".equals(key)) {
				kehuNumBuffer.append(mCustInfos.get(key).title + mCustInfos.get(key).content);
			} else if ("9".equals(key)) {
				kehuNumBuffer.append(mCustInfos.get(key).title + mCustInfos.get(key).content + "】");
			} else if ("16".equals(key)) {
				/**
				 * @16 = "";// '账户产品数'
				 * @17 = "";// '移动'
				 * @18 = "";// '固话'
				 * @19 = "";// '宽带'
				 * @20 = "";// '其它'
				 */
				zhanghuNumBuffer.append(mCustInfos.get(key).content + " 【");
			} else if ("17".equals(key)) {
				zhanghuNumBuffer.append(mCustInfos.get(key).title + mCustInfos.get(key).content);
			} else if ("18".equals(key)) {
				zhanghuNumBuffer.append(mCustInfos.get(key).title + mCustInfos.get(key).content);
			} else if ("19".equals(key)) {
				zhanghuNumBuffer.append(mCustInfos.get(key).title + mCustInfos.get(key).content);
			} else if ("20".equals(key)) {
				zhanghuNumBuffer.append(mCustInfos.get(key).title + mCustInfos.get(key).content + "】");
			} else if ("21".equals(key)) {// @21 = "";// '账户产品清单'
				zhanghuList = mCustInfos.get(key).content;
			} else if ("22".equals(key)) {// @22 = "";// '客户产品清单'
				kehulist = mCustInfos.get(key).content;
			} else {
				if (!"1".equals(key)) {
					LinearLayout ll = customerView.addRow(mCustInfos.get(key).title, mCustInfos.get(key).content);
					if (mCustInfos.containsKey("5") || mCustInfos.containsKey("16")) {
						ll.setBackgroundResource(R.drawable.roundlist_body);
					} else {
						ll.setBackgroundResource(R.drawable.roundlist_tail);
					}
				}
			}

		}
		customerView.fixContent(name, type, manager);
		if (mCustInfos.containsKey("5")) {
			customerView.addRow(mCustInfos.get("5").title, kehuNumBuffer.toString()).setBackgroundResource(R.drawable.roundlist_body);
			LinearLayout ll = customerView.addRow(mCustInfos.get("5").title, kehulist, true);
			if (mCustInfos.containsKey("16")) {
				ll.setBackgroundResource(R.drawable.roundlist_body);
			} else {
				ll.setBackgroundResource(R.drawable.roundlist_tail);
			}
		}
		if (mCustInfos.containsKey("16")) {
			customerView.addRow(mCustInfos.get("16").title, zhanghuNumBuffer.toString()).setBackgroundResource(R.drawable.roundlist_body);
			customerView.addRow(mCustInfos.get("16").title, zhanghuList, true).setBackgroundResource(R.drawable.roundlist_tail);
		}

	}

	private void updateAccountInfoList(Message msg) {
		setLoadState(5, true);
		@SuppressWarnings("unchecked")
		List<AccountInfo> tems = (List<AccountInfo>) msg.obj;
		if (null != msg.getData()) {
			Bundle bundle = msg.getData();
			String acc_nbr = bundle.getString("acc_nbr");
			String cur_month = bundle.getString("cur_month");
			String last_month = bundle.getString("last_month");
			String last_two_month = bundle.getString("last_two_month");
			accountInfoPageView.initTitle(acc_nbr, cur_month, last_month, last_two_month);
		}
		boolean flag = false;
		accountInfoPageView.setEmptyVisibility(View.GONE);
		accountInfoPageView.getLoadMoreButton().setEnabled(true);
		if (currentPage == 1) {
			if (tems.size() < 1) {
				accountInfoPageView.setEmptyVisibility(View.VISIBLE);
				accountInfoPageView.getLoadMoreButton().setEnabled(false);
			}
			if (tems.size() < BASE_PAGE_SIZE) {
				flag = true;
				for (int i = tems.size(); i < BASE_PAGE_SIZE; i++) {
					tems.add(new AccountInfo());
				}
			}
		}
		accountInfoPageView.setAccountInfos(tems);
		if ((tems.size() < PAGE_SIZE) || flag) {
			accountInfoPageView.getLoadMoreButton().setText("数据已全部获取完毕");
			accountInfoPageView.getLoadMoreButton().setEnabled(false);
		} else {
			accountInfoPageView.getLoadMoreButton().setText("查看更多...");
			currentPage++;
			accountInfoPageView.setCurrentPage(currentPage);
		}
	}

	private LinearLayout initSalesTableView() {
		mBuSalesView = new BuSalesView(this);

		BuSalesItem buSalesItem = new BuSalesItem(this);
		buSalesItem.setBackgroud(R.drawable.row0);
		buSalesItem.setValue("序号", "套餐名称", "生效\n日期", "失效\n日期", "到期\n日期");
		buSalesItem.setContentGravityCenter();
		buSalesItem.setTextColor(getResources().getColor(R.color.white));
		mBuSalesView.addTitle(buSalesItem);
		return mBuSalesView;
	}

	private void updateSalesBaseInfoList(Message msg) {
		setLoadState(2, true);
		@SuppressWarnings("unchecked")
		ArrayList<SalesBaseInfo> mProducts = (ArrayList<SalesBaseInfo>) msg.obj;
		for (SalesBaseInfo item : mProducts) {
			if (StringUtils.isEmpety(item.pos) || StringUtils.isEmpety(item.getOfferName())) {
				continue;
			}
			BuSalesItem buSalesItem1 = new BuSalesItem(this);
			buSalesItem1.setValue(item.pos, item.getOfferName(), mulLineYMD(item.effDate), mulLineYMD(item.expDate), mulLineYMD(item.agreeDate));
			mBuSalesView.addContent(buSalesItem1);
		}

		if (mProducts.size() < BASE_PAGE_SIZE) {
			for (int index = mProducts.size(); index < BASE_PAGE_SIZE; index++) {
				BuSalesItem buSalesItem1 = new BuSalesItem(this);
				buSalesItem1.setValue("", "", "", "", "");
				mBuSalesView.addContent(buSalesItem1);
			}
		}

	}

	private void updateAttrBaseInfoList(Message msg) {
		setLoadState(3, true);
		@SuppressWarnings("unchecked")
		ArrayList<AttrBaseInfo> tems = (ArrayList<AttrBaseInfo>) msg.obj;
		List<String> data = new ArrayList<String>();
		if (null != msg.getData()) {
			for (AttrBaseInfo item : tems) {
				data.clear();
				data.add(item.pos);
				data.add(item.title.trim() + SPLIT_BLANK + SPLIT_BLANK);
				data.add(SPLIT_BLANK_SUB + item.content + SPLIT_BLANK_SUB);
				attrTableView.addTableRow(data, false, false, -1);
			}
		}
		if (tems.size() < BASE_PAGE_SIZE) {
			for (int index = tems.size(); index < BASE_PAGE_SIZE; index++) {
				data.clear();
				data.add("");
				data.add("");
				data.add("");
				attrTableView.addTableRow(data, false);
			}
		}
	}

	private TableFormView getAttrTableView() {
		attrTableView = new TableFormView(this);
		attrTableView.setColumnStretchable(1, true);
		attrTableView.setColumnShrinkable(0, true);
		ArrayList<String> titles = new ArrayList<String>();
		titles.add("序号");
		titles.add(SPLIT_BLANK + "套餐名称" + SPLIT_BLANK);
		titles.add(SPLIT_BLANK_SUB + "生效日期" + SPLIT_BLANK_SUB);
		attrTableView.initTableTitle(titles);
		attrTableView.setColumnShrinkable(0, true);
		attrTableView.setColumnStretchable(1, true);
		attrTableView.setColumnStretchable(2, true);
		attrTableView.setColumnShrinkable(1, true);
		return attrTableView;
	}

	private TableFormView mMonthTable;
	private BMonthView mMonthView;

	private BMonthView getMonthTableView() {
		initNowDate();
		mMonthView = new BMonthView(this);
		setMonthDate();
		mMonthView.setBtnDateListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				showDateDialog();
			}
		});
		mMonthTable = mMonthView.getMonthTable();
		mMonthTable.setColumnShrinkable(0, true);
		mMonthTable.setColumnStretchable(1, true);
		mMonthTable.setColumnShrinkable(1, true);
		initMonthTable();
		return mMonthView;
	}

	private void updateMonthBaseInfoList(Message msg) {
		setLoadState(4, true);
		if (bInitMonth) {
			Utils.debug("test", "clear month table bInit month " + bInitMonth);
			mMonthTable.resetTableRow();
			initMonthTable();
			stopWait();
		}

		@SuppressWarnings("unchecked")
		ArrayList<MonthAcctBaseInfo> tems = (ArrayList<MonthAcctBaseInfo>) msg.obj;
		if (tems.size() == 0) {
			/**
			 * disable the button of selector date when the result is null
			 * mMonthView.getBtnDate().setVisibility(View.GONE);
			 */
			Utils.toast(this, getString(R.string.no_data_month), false);
		} else {
			year = tempYear;
			month = tempMonth;
			setMonthDate();
		}
		List<String> data = new ArrayList<String>();
		for (MonthAcctBaseInfo item : tems) {
			data.clear();
			data.add(String.valueOf(item.pos));
			data.add(item.title + SPLIT_BLANK + SPLIT_BLANK_SUB);
			data.add(SPLIT_BLANK_SUB + item.content + SPLIT_BLANK_SUB);
			mMonthTable.addTableRow(data, false, true, -1);
		}
		if (tems.size() < BASE_PAGE_SIZE) {
			for (int index = tems.size(); index < BASE_PAGE_SIZE; index++) {
				data.clear();
				data.add("");
				data.add("");
				data.add("");
				mMonthTable.addTableRow(data, false);
			}
		}

	}

	private void initMonthTable() {
		ArrayList<String> titles = new ArrayList<String>();
		titles.add("序号");
		titles.add(SPLIT_BLANK + "名称" + SPLIT_BLANK);
		titles.add(SPLIT_BLANK_SUB + "费用" + SPLIT_BLANK_SUB);
		mMonthTable.setColumnShrinkable(0, true);
		mMonthTable.initTableTitle(titles);
	}

	private void setMonthDate() {
		String date = year + "-" + month;
		mMonthView.setDatePromp(date);
	}

	private String year = "";
	private String month = "";

	private String tempYear = "";
	private String tempMonth = "";

	private void initNowDate() {
		SimpleDateFormat tDateFormat = com.jxs.android.util.Timer.getSDFyyyyMM();
		String tm = tDateFormat.format(new Date());
		year = tm.substring(0, 4);
		int pa = (Integer.valueOf(tm.substring(4, 6)) - 1);
		if (pa == 0) {
			pa = 12;
			year = (Integer.valueOf(year) - 1) + "";
		}
		month = new DecimalFormat("00").format(pa);
		tempMonth = month;
		tempYear = year;
	}

	private boolean bInitMonth = false;

	private void showDateDialog() {
		WheelDateDialog dateDialog = new WheelDateDialog(this, 0x8787, getDateYM() + "01");
		dateDialog.setDateChangeListener(new WheelDateDialog.DateChangeListener() {

			@Override
			public void onKeySave(int requestCode, String dateTimeValue) {

				int year = Integer.valueOf(dateTimeValue.substring(0, 4));
				int monthOfYear = Integer.valueOf(dateTimeValue.substring(4, 6));
				int dayOfMonth = Integer.valueOf(dateTimeValue.substring(6, 8));

				if (Utils.compareCurDate(year, monthOfYear, dayOfMonth) > 0) {
					errorMsgPop(R.string.invalid_date);
					return;
				}
				String month = "";
				int monthValue = monthOfYear;
				if (monthValue < 10) {
					month = "0" + String.valueOf(monthValue);
				} else {
					month = String.valueOf(monthValue);
				}
				bInitMonth = true;
				showWait();
				getMonthInfo(year + "" + month);
				tempYear = year + "";
				tempMonth = month;

			}
		});
		dateDialog.show();
		dateDialog.dayGone();
	}

	private void getMonthInfo(String dateYM) {
		getMonthAcctBaseInfoHolder.getMonthAcctBaseInfo(WeixiApplication.getApplication().getUserId(), mServId, dateYM);
	}

	private BuLoadingDailog waitDialog;

	public void initWaitDialog() {
		if (waitDialog == null) {
			waitDialog = new BuLoadingDailog(this);
			waitDialog.setCancelable(true);
		}
	}

	private void showWait() {
		waitDialog.show();
		waitDialog.showLoading(R.string.load_and_wait);
	}

	private void stopWait() {
		if (waitDialog != null && waitDialog.isShowing()) {
			waitDialog.hide();
		}
	}

	@Override
	protected void onStop() {

		if (waitDialog != null) {
			waitDialog.dismiss();
		}
		super.onStop();
	}

	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {
	}

	@Override
	public void onPageSelected(int position) {
		mTabIndex = position;
		setRdBtnStateByPos(position);
	}

	private void setRdBtnStateByPos(int pos) {
		if (pos >= 0 && pos < 6) {
			RadioButton rdBtn = (RadioButton) radioGroup.getChildAt(pos);
			rdBtn.setChecked(true);
		}
	}

	private String getDateYM() {
		return year + month;
	}

	private String mulLineYMD(String ymd) {
		if (ymd.length() > 4) {
			String year = ymd.substring(0, 4);
			String md = ymd.substring(4, ymd.length());
			Utils.debug("test", year + "-" + md);
			return year + "\n" + md;
		} else {
			return ymd;
		}

	}

}
